﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SnakeWalk.Painter;
using SnakeWalk.Utils;

namespace SnakeWalk
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
            ResizePanel();
        }

        private void ResizePanel()
        {
            this.Width = (int)SizeAndBaitSnake.Width * 60 + 12 + 1;
            this.Height = (int)SizeAndBaitSnake.Width * 45 + 89 + 1;
        }

        SnakePainter painter;
        GameSnake gameSnake;

        private void NewGameMenuItem_Click(object sender, EventArgs e)
        {
            this.TimerMoveSnake.Tick -= new System.EventHandler(this.TimerMoveSnake_Tick);
            this.TimerMoveSnake.Tick += new System.EventHandler(this.TimerMoveSnake_Tick);

            gameSnake = new GameSnake(new SWSize(PanelSnake.Size.Width, PanelSnake.Size.Height));

            gameSnake.NewGame();

            painter = new SnakePainter(gameSnake.Snake, gameSnake.Bait, PanelSnake);

            TimerMoveSnake.Enabled = true;
            TimeCountControl.BeginCount();
        }

        private void TimerMoveSnake_Tick(object sender, EventArgs e)
        {
            gameSnake.Run();
            ScoreCountControl.Score = gameSnake.GameScore.ToString();

            if (gameSnake.GameStateSnake == GameSate.Win)
            {
                TimeCountControl.ResetCount();
                TimerMoveSnake.Enabled = false;
                TimerMoveSnake.Stop();
                MessageBox.Show("You Win !!!");
            }

            if (gameSnake.GameStateSnake == GameSate.Stop)
            {
                TimeCountControl.ResetCount();
                TimerMoveSnake.Enabled = false;
                TimerMoveSnake.Stop();
                PanelSnake.CreateGraphics().Clear(Color.Beige);
            }

            if (gameSnake.GameStateSnake == GameSate.GameOver)
            {
                TimeCountControl.ResetCount();
                TimerMoveSnake.Enabled = false;
                TimerMoveSnake.Stop();
                MessageBox.Show("Game Over !!!");
            }
            else
            {
                painter.PaintSnake();
                painter.PaintBait();
            }
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            this.KeyPreview = true;
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Left:
                    gameSnake.ChangeDirection(DirectionSnake.Left);
                    break;
                case Keys.Right:
                    gameSnake.ChangeDirection(DirectionSnake.Right);
                    break;
                case Keys.Up:
                    gameSnake.ChangeDirection(DirectionSnake.Up);
                    break;
                case Keys.Down:
                    gameSnake.ChangeDirection(DirectionSnake.Down);
                    break;
                default:
                    return base.ProcessCmdKey(ref msg, keyData);
            }
            return true;
        }

        private void StopMenuItem_Click(object sender, EventArgs e)
        {
            gameSnake.StopGame(false);
        }
    }
}
